package com.farm.code.server;


import com.farm.core.auth.domain.LoginUser;
import com.farm.core.time.TimeTool;

import org.apache.commons.beanutils.BeanUtils;
import com.farm.code.dao.FarmCodeTempletDaoInter;
import com.farm.code.domain.FarmCodeTemplet;
import com.farm.code.server.FarmCodeTempletManagerInter;
import com.farm.core.sql.query.DataQuery;

/**
 * 模板信息
 * 
 * @author MAC_wd
 */
public class FarmCodeTempletManagerImpl implements FarmCodeTempletManagerInter {
	private FarmCodeTempletDaoInter farmCodeTempletDao;
//	private static final Logger log = Logger
//			.getLogger(FarmCodeTempletManagerImpl.class);

	@Override
	public FarmCodeTemplet insertFarmCodeTempletEntity(FarmCodeTemplet entity,
			LoginUser user) {
		entity.setCuser(user.getId());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setCusername(user.getName());
		entity.setEuser(user.getId());
		entity.setEusername(user.getName());
		entity.setEtime(TimeTool.getTimeDate14());
		entity.setPstate("1");
		return farmCodeTempletDao.insertEntity(entity);
	}

	@Override
	public FarmCodeTemplet editFarmCodeTempletEntity(FarmCodeTemplet entity,
			LoginUser user) {
		FarmCodeTemplet entity2 = farmCodeTempletDao.getEntity(entity.getId());
		entity2.setEuser(user.getId());
		entity2.setEusername(user.getName());
		entity2.setEtime(TimeTool.getTimeDate14());
		// entity2.setPstate(entity.getPstate());
		// entity2.setPcontent(entity.getPcontent());
		// entity2.setPackid(entity.getPackid());
		entity2.setTemcontent(entity.getTemcontent().replaceAll(" "," "));
		entity2.setFilename(entity.getFilename());
		entity2.setTemname(entity.getTemname());
		farmCodeTempletDao.editEntity(entity2);
		return entity2;
	}

	@Override
	public void deleteFarmCodeTempletEntity(String entity, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		farmCodeTempletDao.deleteEntity(farmCodeTempletDao.getEntity(entity));
	}

	@Override
	public FarmCodeTemplet getFarmCodeTempletEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return farmCodeTempletDao.getEntity(id);
	}

	@Override
	public DataQuery createFarmCodeTempletSimpleQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		DataQuery dbQuery = DataQuery.init(query, "farm_code_templet",
				"id,PSTATE,PCONTENT,PACKID,TEMCONTENT,FILENAME,TEMNAME");
		return dbQuery;
	}

	// ----------------------------------------------------------------------------------
	public FarmCodeTempletDaoInter getfarmCodeTempletDao() {
		return farmCodeTempletDao;
	}

	public void setfarmCodeTempletDao(FarmCodeTempletDaoInter dao) {
		this.farmCodeTempletDao = dao;
	}

	@Override
	public void copyTemplet(String templetid, String packid,
			LoginUser currentUser) {
		FarmCodeTemplet newTem = new FarmCodeTemplet();
		try {
			BeanUtils.copyProperties(newTem,
					farmCodeTempletDao.getEntity(templetid));
			newTem.setPackid(packid);
			farmCodeTempletDao.insertEntity(newTem);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
